import pymysql

host_ip = "172.20.107.239"
username = "root"
passwd = "root"
database = "RadarSSD"
table = "RadarSSD.Signal"

def connect_db():
    conn=pymysql.connect(host=host_ip,
                         user=username,
                         password=passwd,
                         database=database,
                         charset="utf8")
    
    return conn

def build_insert_query(TOA, DOA, RF):
    return "insert %s values (%d,%d,%d);" % (table,TOA,DOA,RF)

def build_select_query(start_TOA=0, end_TOA=10000000000, limit=100):
    return "SELECT TOA from %s where TOA<=%d and TOA>=%d LIMIT %d;" % (table, end_TOA, start_TOA, limit)

def build_cleardb_query():
    return "delete from %s" % table

if __name__ == "__main__":
    conn = connect_db()
    cursor = conn.cursor()

    #查询测试
    cursor.execute(build_select_query())
    datas = cursor.fetchall()
    print(datas)

    #清空测试
    cursor.execute(build_cleardb_query())
    conn.commit()
    cursor.execute(build_select_query())
    datas = cursor.fetchall()
    print(datas)

    #插入测试
    cursor.execute(build_insert_query(12,32,43))
    cursor.execute(build_insert_query(121,322,143))
    conn.commit()
    cursor.execute(build_select_query())
    datas = cursor.fetchall()
    print(datas)